package de.tudarmstadt.ukp.wikisniffer.userstudy.evaluation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class VpEvaluator {
	
/**
 * 	vpcode
 * 	sniffer_on_off_1
 * 	thema_1
 * 	time_stop_thema_1
 * 	frage_1_1
 * 	frage_1_1_answer
 * 	frage_1_2
 * 	frage_1_2_answer
 * 	frage_1_3
 * 	frage_1_3_answer
 * 	frage_1_4
 * 	frage_1_4_answer
 * 	thema_2
 * 	time_stop_thema_2
 * 	frage_2_1
 * 	frage_2_1_answer
 * 	frage_2_2
 * 	frage_2_2_answer
 * 	frage_2_3
 * 	frage_2_3_answer
 * 	frage_2_4
 * 	frage_2_4_answer
 * 	thema_3
 * 	time_stop_thema_3
 * 	frage_3_1
 * 	frage_3_1_answer
 * 	frage_3_2
 * 	frage_3_2_answer
 * 	frage_3_3
 * 	frage_3_3_answer
 * 	frage_3_4
 * 	frage_3_4_answer
 * 	sniffer_on_off_2
 * 	thema_4
 * 	time_stop_thema_4
 * 	frage_4_1
 * 	frage_4_1_answer
 * 	frage_4_2
 * 	frage_4_2_answer
 * 	frage_4_3
 * 	frage_4_3_answer
 * 	frage_4_4
 * 	frage_4_4_answer
 * 	thema_5
 * 	time_stop_thema_5
 * 	frage_5_1
 * 	frage_5_1_answer
 * 	frage_5_2
 * 	frage_5_2_answer
 * 	frage_5_3
 * 	frage_5_3_answer
 * 	frage_5_4
 * 	frage_5_4_answer
 * 	thema_6
 * 	time_stop_thema_6
 * 	frage_6_1
 * 	frage_6_1_answer
 * 	frage_6_2
 * 	frage_6_2_answer
 * 	frage_6_3
 * 	frage_6_3_answer
 * 	frage_6_4
 * 	frage_6_4_answer
**/	
	private HashMap<String,String> properties;

	private StringBuffer buffer;
	private String seperator;
	
	public static VpEvaluator vpEvaluator;
	
	public VpEvaluator(File file) throws IOException {
		vpEvaluator = this;
		properties = new HashMap<String,String>();
		StringBuffer fileData = new StringBuffer(1000);
		BufferedReader reader = new BufferedReader(
				new FileReader(file));
		char[] buf = new char[1024];
		int numRead=0;
		while((numRead=reader.read(buf)) != -1){
			fileData.append(buf, 0, numRead);
		}
		reader.close();
		
		storeProperties(fileData.toString().split("\n"));
	}

	private void storeProperties(String[] propertiesAsArray) {
		int n=0;
		
		addToProperties("vpcode" ,  getFirst(propertiesAsArray[1]));
		addToProperties("sniffer_on_off_1" ,  getFirst(propertiesAsArray[3]));
		addToProperties("sniffer_on_off_2" ,  getFirst(propertiesAsArray[7]));

		addToProperties("thema_1" ,  getTopicCode(getFirst(propertiesAsArray[4])));
		addToProperties("thema_2" ,  getTopicCode(getFirst(propertiesAsArray[5])));
		addToProperties("thema_3" ,  getTopicCode(getFirst(propertiesAsArray[6])));
		addToProperties("thema_4" ,  getTopicCode(getFirst(propertiesAsArray[8])));
		addToProperties("thema_5" ,  getTopicCode(getFirst(propertiesAsArray[9])));
		addToProperties("thema_6" ,  getTopicCode(getFirst(propertiesAsArray[10])));
		
		List<GroupedAnswer> questionGroups = new ArrayList<GroupedAnswer>();
		int lastStart = 11;
		int lastFinish = 0;
		for(n=12;n<propertiesAsArray.length;n++){
			if(getFirst(propertiesAsArray[n]).startsWith("Started preparing")){
				lastFinish = n-1;
				questionGroups.add(new GroupedAnswer(propertiesAsArray, lastStart, lastFinish));
				lastStart = n;
			}
		}
		//do it one more time for the last occurrence		
		questionGroups.add(new GroupedAnswer(propertiesAsArray, lastStart, propertiesAsArray.length-1));
		
		
		for(int k = 1 ; k<=6; k++){
			questionGroups.get(questionGroups.size()+k-7).addToProperties(k);
			System.out.println("K="+k);
		}
		
	}

	private String getTopicCode(String topic) {
		topic = topic.trim();
		
		if(topic.endsWith(", Bildung")){
			return "T4";
		}
		if(topic.endsWith(", OECD")){
			return "T1";
		}
		if(topic.endsWith("dagogik")){
			return "T2";
		}
		if(topic.endsWith(", soziale Kompetenz")){
			return "T3";
		}
		if(topic.endsWith(", Erzieher")){
			return "T5";
		}
		if(topic.endsWith(", Berufsbildungsgesetz")){
			return "T6";
		}
		return "not defined";
	}

	private String getFirst(String string) {
		
		return string.split("\t")[0];
	}

	public String getOutputString(String seperator) {
		buffer = new StringBuffer();
		this.seperator = seperator;
		addToBuffer("vpcode");
		addToBuffer("sniffer_on_off_1");
		addToBuffer("thema_1");
		addToBuffer("time_stop_thema_1");
		addToBuffer("frage_1_1");
		addToBuffer("frage_1_1_answer");
		addToBuffer("frage_1_2");
		addToBuffer("frage_1_2_answer");
		addToBuffer("frage_1_3");
		addToBuffer("frage_1_3_answer");
		addToBuffer("frage_1_4");
		addToBuffer("frage_1_4_answer");
		addToBuffer("thema_2");
		addToBuffer("time_stop_thema_2");
		addToBuffer("frage_2_1");
		addToBuffer("frage_2_1_answer");
		addToBuffer("frage_2_2");
		addToBuffer("frage_2_2_answer");
		addToBuffer("frage_2_3");
		addToBuffer("frage_2_3_answer");
		addToBuffer("frage_2_4");
		addToBuffer("frage_2_4_answer");
		addToBuffer("thema_3");
		addToBuffer("time_stop_thema_3");
		addToBuffer("frage_3_1");
		addToBuffer("frage_3_1_answer");
		addToBuffer("frage_3_2");
		addToBuffer("frage_3_2_answer");
		addToBuffer("frage_3_3");
		addToBuffer("frage_3_3_answer");
		addToBuffer("frage_3_4");
		addToBuffer("frage_3_4_answer");
		addToBuffer("sniffer_on_off_2");
		addToBuffer("thema_4");
		addToBuffer("time_stop_thema_4");
		addToBuffer("frage_4_1");
		addToBuffer("frage_4_1_answer");
		addToBuffer("frage_4_2");
		addToBuffer("frage_4_2_answer");
		addToBuffer("frage_4_3");
		addToBuffer("frage_4_3_answer");
		addToBuffer("frage_4_4");
		addToBuffer("frage_4_4_answer");
		addToBuffer("thema_5");
		addToBuffer("time_stop_thema_5");
		addToBuffer("frage_5_1");
		addToBuffer("frage_5_1_answer");
		addToBuffer("frage_5_2");
		addToBuffer("frage_5_2_answer");
		addToBuffer("frage_5_3");
		addToBuffer("frage_5_3_answer");
		addToBuffer("frage_5_4");
		addToBuffer("frage_5_4_answer");
		addToBuffer("thema_6");
		addToBuffer("time_stop_thema_6");
		addToBuffer("frage_6_1");
		addToBuffer("frage_6_1_answer");
		addToBuffer("frage_6_2");
		addToBuffer("frage_6_2_answer");
		addToBuffer("frage_6_3");
		addToBuffer("frage_6_3_answer");
		addToBuffer("frage_6_4");
		addToBuffer("frage_6_4_answer");
		return buffer.toString();
	}

	private void addToBuffer(String key) {
		buffer.append(properties.get(key));
		buffer.append(seperator);
	}
	
	public void addToProperties(String key,String value){
		properties.put(key, value);
	}

}
